import 'package:flutter/foundation.dart';
import 'package:flutterstudyapplast/business/models/article_item.dart';
import 'package:flutterstudyapplast/business/models/banner_item.dart';
import 'package:flutterstudyapplast/services/article/article_service.dart';
import 'package:flutterstudyapplast/services/base_viewmodel.dart';
import 'package:flutterstudyapplast/services/service_locator.dart';

class ArticleViewModel extends BaseViewModel with ChangeNotifier{
  final ArticleService _service = serviceLocator<ArticleService>();

  int _totalCount = 0;
  List<ArticleItem> _articles = [];
  List<BannerItem> _banners = [];
  List<ArticleItem> get articles => _articles;
  List<BannerItem> get banners => _banners;
  int get totalCount => _totalCount;

  Future<void> loadData(int page) async{
    var result =  await _service.getArticleList(page);
    _totalCount = result["total"];
    if(page == 0){
      _articles.clear();
    }
    _articles.addAll(result["datas"]);

    notifyListeners();
    return null;
  }
  Future<void> getBanner() async{
    var result = await _service.getBanner();
    _banners.clear();
    _banners.addAll(result);
    notifyListeners();
    return null;
  }
}